package com.example.demo.jucConcurrent.CyclicBarrier详解;

import java.util.concurrent.BrokenBarrierException;
import java.util.concurrent.CyclicBarrier;

public class CyclicBarrierDemo {
    public static void main(String[] args) throws BrokenBarrierException, InterruptedException {
        CyclicBarrier cb = new CyclicBarrier(3, new Thread("barrierAction") {

            @Override
            public void run() {
                System.out.println(Thread.currentThread().getName() + " barrier action");

            }
        });

        MyThread t1 = new MyThread("t1", cb);
        MyThread t2 = new MyThread("t2", cb);
        t1.start();
        t2.start();
        System.out.println(Thread.currentThread().getName());
        cb.await();
        System.out.println(Thread.currentThread().getName());

    }
}

class MyThread extends  Thread{
    private CyclicBarrier cb;

    public MyThread(String name,CyclicBarrier cb) {
        this.cb = cb;
    }

    @Override
    public void run() {
        System.out.println(Thread.currentThread().getName()+"going TO AWAIT");
        try {
            cb.await();
            System.out.println(Thread.currentThread());
        } catch (Exception e) {
            e.printStackTrace();
        }

    }
}